package com.cinemtl.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.MailManager;

import com.cinemtl.action.UserAction;
import com.cinemtl.beans.User;

/**
 * Servlet implementation class Signup
 */
@WebServlet("/signup")
public class Signup extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Signup() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String fname = request.getParameter("fname");
		String lname = request.getParameter("lname");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String birthday = request.getParameter("birthday");
		String adress = request.getParameter("adress");
		String phone = request.getParameter("phone");
		String sex = request.getParameter("sex");
		DateFormat format = new SimpleDateFormat("dd/mm/yyyy");
		Date date = null;
		try {
			date = format.parse(birthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setEmail(email);
		user.setFname(fname);
		user.setLname(lname);
		user.setBirthday(date);
		user.setSex(sex);
		user.setTel(phone);
		user.setAdress(adress);
		UserAction.addUser(user);
		MailManager.sendWelcomeMail(lname, email);
		response.sendRedirect("home.jsp");
	}

}
